﻿using BCL.BaseLib;
using BCL.BaseLib.Basics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;


namespace BCL.WPFComponent
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window, ICustom, IMainForm
    {
        private List<FunctionEntity> openfe;
        private IFunctionControl topcontrol;

        public List<FunctionEntity> OpenedFe
        {
            get { return openfe; }
            set { openfe = value; }
        }


        public MainWindow()
        {
            InitializeComponent();
        }
        public MainWindow(List<FunctionEntity> fes)
        {
            MaxHeight = SystemParameters.WorkArea.Height + 7;
            MaxWidth = SystemParameters.WorkArea.Width + 7;
            BCLApplication.MainForm = this;
            openfe = new List<FunctionEntity>();
            RegisteFunctions(fes);
            InitializeComponent();
            
        }
        private void RegisteFunctions(List<FunctionEntity> feList)
        {
            List<FunctionEntity> appFunMunes = new List<FunctionEntity>();
            if (feList != null)
            {
                foreach (FunctionEntity item in feList)
                    appFunMunes.AddRange(FunctionToList(item));
            }
            BCLApplication.AppFunMunes = appFunMunes;
            BCLApplication.AppFunTree = feList;
        }
        /// <summary>
        /// 将树形菜单转换成离散菜单
        /// </summary>
        /// <param name="fe"></param>
        /// <returns></returns>
        private List<FunctionEntity> FunctionToList(FunctionEntity fe)
        {
            List<FunctionEntity> list = new List<FunctionEntity>();
            
            if (fe.Items != null)
                foreach (FunctionEntity item in fe.Items)
                    list.AddRange(FunctionToList(item));
            list.Add(fe);
            return list;
        }


        public IFunctionControl TopControl { get=> topcontrol; set =>MainTopControl.TapControl.Child=(Control)(topcontrol=value); }
        public IFunctionControl FootControl { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }


        public void CloseFunction(FunctionEntity info)
        {
            if (info == null)
                return;
            MainControl.CloseFunction(info);
            if (openfe.Contains(info))
                openfe.Remove(info);
        }

        public void CloseFunction(string appId)
        {
            FunctionEntity fe = BCLApplication.AppFunMunes?.FirstOrDefault(o => o.AppId == appId);
            CloseFunction(fe);
        }

        public bool FunctionInvoke(FunctionEntity fe)
        {
            throw new NotImplementedException();
        }

        public void Msg(FunctionEntity fe, object param)
        {
            throw new NotImplementedException();
        }

        public void OpenFunction(FunctionEntity info)
        {
            if (info == null)
                return;
            if (info.ItemType != FunctionItemType.Item)
                return;
            if (info.FunctionType == FunctionType.Function)
            {
                info.Custom.FunctionInvoke(info);
                return;
            }
            MainControl.FunctionInvoke(info);
            CanvasMune.Visibility = Visibility.Hidden;
            if (!openfe.Contains(info))
                openfe.Add(info);
        }

        public void OpenFunction(string appId)
        {
            FunctionEntity fe = BCLApplication.AppFunMunes?.FirstOrDefault(o => o.AppId == appId);
            OpenFunction(fe);
        }



        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
                base.DragMove();
        }

        public void OpenMenu()
        {
            CanvasMune.Visibility = CanvasMune.Visibility == Visibility.Hidden ? Visibility.Visible : Visibility.Hidden;
            if (CanvasMune.Visibility == Visibility.Visible)
                CanvasMune.Focus();
        }

        private void CanvasMune_LostMouseCapture(object sender, MouseEventArgs e)
        {
            CanvasMune.Visibility = Visibility.Hidden;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            if (BCLApplication.AppFunMunes == null && BCLApplication.AppFunMunes.Count == 0)
                return;

            foreach (FunctionEntity item in BCLApplication.AppFunMunes)
            {
                if (item.ItemType == FunctionItemType.Item &&item.StartLoad)
                {
                    if (item.FunctionType == FunctionType.Control)
                        OpenFunction(item);
                    else if (item.FunctionType == FunctionType.Function)
                        item.Custom?.FunctionInvoke(item);
                }
            }
            //加载首页
            OpenFunction(BCLApplication.AppFunMunes.FirstOrDefault(o => o.Index));
        }
    }
}
